home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 117 / PC Guia 117.iso / Software / Utils / Software6 / Product13 / googlebar-0.9.5.06-fx.xpi / chrome / googlebar.jar / content / uninstallGoogleBar.js < prev   
Text File  |  2003-04-03  |  12KB  |  329 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Mouse googlebar for Mozilla.
  15.  *
  16.  * The Initial Developer of the Original Code is Pavol Vaskovic.
  17.  * Portions created by the Initial Developer are Copyright (C) 2001
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Chase Tingley <tingley@sundell.net>
  22.  *  David Perry <d.perry@utoronto.ca>
  23.  *  Pavol Vaskovic <pali@pali.sk>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. // semaphore for synchronizig with RDF changes, so that we do not shut down
  40. // sooner then everything is safely written to disk
  41. var semaphore = 4; // there will be 4 async operations
  42. var timerID;
  43. var bundle, googlebarRemoved, dirNotDeleted;
  44. const DEBUG_USE_PROFILE = PR_TRUE = 1;
  45. var ChromeRegistry = null ;
  46. var RDFU = null ;
  47.  
  48.  
  49.  
  50. function showWebPage(pageUrl) {
  51.   //check for an existing browser window
  52.   const kWindowMediatorContractID = "@mozilla.org/rdf/datasource;1?name=window-mediator";
  53.   const kWindowMediatorIID = Components.interfaces.nsIWindowMediator;
  54.   const kWindowMediator = Components.classes[kWindowMediatorContractID].getService(kWindowMediatorIID);
  55.   var browserWindow = kWindowMediator.getMostRecentWindow("navigator:browser");
  56.   if (browserWindow) {
  57.     try {
  58.       browserWindow.delayedOpenTab(pageUrl);
  59.     }
  60.     catch(e) {
  61.       browserWindow.loadURI(pageUrl);
  62.     }
  63.   }
  64.   else {
  65.     window.open(pageUrl);
  66.   }
  67. }
  68.  
  69.  
  70.  
  71. function confirmUninstall(){
  72. /* initialize strings first
  73.   document.getElementById('bundle_googlebar');
  74.  // bundle = document.getElementById("bundle_googlebar");
  75.   googlebarRemoved = bundle.getString("bundle_googlebar");
  76.   dirNotDeleted = bundle.getFormattedString("dirNotDeleted", ["'googlebar'"], 1);
  77. */
  78.   // ask user if he really wants it
  79.   // if (confirm(bundle.getString("uninstallWarning")))
  80. if (confirm("uninstallWarning"))
  81.   
  82.     googlebarUninstall();
  83.  
  84. }
  85.  
  86. function googlebarUninstall(){
  87.   dump("Googlebar: googlebarUninstall\n");
  88.   // this is 'correct' way to uninstall, modified from skin uninstallation code
  89.   // untill .uninstallPackage gets implemented (bug XXXXX) - we'll HACK for now 
  90.   const DEBUG_USE_PROFILE = true; //?
  91.  
  92.  
  93. //  try {
  94.  
  95. RDFU = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService);
  96. //          .getService(Components.interfaces.nsIRDFService);
  97. //    chromeRegistry = Components.classes["@mozilla.org/chrome/chrome-registry;1"]
  98. //        .getService(Components.interfaces.nsIXULChromeRegistry);
  99.  
  100.  
  101. //      if (chromeRegistry)
  102. //      chromeRegistry = chromeRegistry.QueryInterface(Components.interfaces.nsIChromeRegistry);
  103. //  }
  104. //  catch(e) {}
  105. //  chromeRegistry.uninstallPackage("googlebar", DEBUG_USE_PROFILE);
  106.  
  107.  
  108. // *****************************************************************************
  109. // *************** everything below is HACK!!! *********************************
  110. // *****************************************************************************
  111.   
  112.   // remove all references to our package from various rdf files, we also delete 
  113.   // 'googlebar' directory in this function, because chromeDir is defined in there
  114.  
  115.  
  116.   removePackageReferences(); 
  117.  
  118.  
  119.   // remove Googlebar preferences
  120.   var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  121.   var rootBranch = pref.getBranch("");
  122.   rootBranch.deleteBranch("googlebar.");
  123.  
  124.  
  125.  
  126.  
  127.  
  128.   // shut down mozilla, that's the only way to get rid of our own overlays
  129.   // running, which would try to read preferences etc...
  130.  
  131.   timerID = setInterval("if (semaphore == 0) { clearInterval(timerID); alert('googlebarRemoved'); goQuitApplication();};",500);
  132. }
  133.  
  134. // overlayRemover removes target from sequence in datasourceURI
  135. // we clean our references from overlay files with these
  136. function overlayRemover(datasourceURI, seq, target) {
  137.   this.mDS = datasourceURI;
  138.   this.mTarget = target;
  139.   this.mSeq = seq;
  140. };
  141. overlayRemover.prototype = {
  142.  
  143.   remove: function()
  144.   {
  145.     RDFU.loadDataSource(this.mDS, this);
  146.   },
  147.  
  148.   onDataSourceReady: function(aDS)
  149.   {
  150.     dump("Googlebar: overlayRemover: Attempting to remove\n\t" + this.mTarget + 
  151.          "\n\tfrom sequence " + this.mSeq+"\n");
  152.     var seq = RDFU.findSeq(aDS, this.mSeq);
  153.     var target = gRDF.GetLiteral(this.mTarget);
  154.     seq.RemoveElement(target, true);
  155.     RDFU.saveDataSource(aDS);
  156.     dump("Googlebar: overlayRemover: OK\n");
  157.     semaphore--; // asyncronous operation finished
  158.   },
  159.  
  160.   onError: function(aStatus, aErrorMsg)
  161.   {
  162.     dump("Googlebar: overlayRemover: Error: status="+aStatus+",", aErrorMsg);
  163.   }
  164. };
  165.  
  166.  
  167.  
  168.  
  169. function removePackageReferences(){
  170.   dump("Googlebar: removePackageReferences\n");
  171.   /////////////////////////////////////////////////////////////////////////
  172.   // This is a sketchy, sketchy way to do it.  But since actual support for
  173.   // loading chrome.rdf via a resource channel seems to be far away, 
  174.   // I'll do this the way that the chrome registry does: 
  175.   // THROUGH FORCE OF WILL ALONE.
  176.  
  177.   // Basically, load the directory service and look up the key "AChrom"
  178.   // (see NS_APP_CHROME_DIR in nsAppDirectoryServiceDefs.h).  This gives
  179.   // us an nsIFile for the chrome directory, from which the url is easily
  180.   // extracted...
  181.  
  182.   // nsAppDirectoryServiceDefs.h contains many other useful magic words for
  183.   // finding other places (prefs, profile, etc).
  184.  
  185.   const kDirServiceCID = Components.ID("{f00152d0-b40b-11d3-8c9c-000064657374}");
  186.   const nsIProperties = Components.interfaces.nsIProperties;
  187.   const nsIFileIID = Components.ID("{c8c0a080-0868-11d3-915f-d9d889d48e3c}");
  188.   const magicChromeKey = "AChrom";
  189.  
  190.   var dirService = Components.classesByID[kDirServiceCID].getService(nsIProperties);
  191.  
  192.   var chromeDir = dirService.get(magicChromeKey, nsIFileIID);
  193.  
  194.   // chromeUrl is the file:// url of the chrome directory, eg
  195.   // file:///home/tingley/src/mozilla/dist/bin/chrome/
  196.   var chromeUrl = "file:///" + chromeDir.path + "/";
  197.   dump("\tchrome url is "+chromeUrl+"\n");
  198.   
  199.   // remove our references from chrome/chrome.rdf
  200.   dump("Attempting to load "+chromeUrl+"chrome.rdf\n");
  201.   RDFU.loadDataSource(chromeUrl + "chrome.rdf", removeFromChrome);
  202.  
  203.   // let's remove our overlays (these were installed from our contents.rdf)
  204.      var overlay1 = new overlayRemover(chromeUrl + 
  205.                  "overlayinfo/communicator/content/overlays.rdf",
  206.                  "chrome://communicator/content/pref/preftree.xul",
  207.                  "chrome://googlebar/content/googlebarPrefOverlay.xul");
  208.   overlay1.remove();
  209.  
  210.   var overlay2 = new overlayRemover(chromeUrl + 
  211.                  "overlayinfo/navigator/content/overlays.rdf",
  212.                  "chrome://navigator/content/navigator.xul",
  213.                  "chrome://googlebar/content/googlebarOverlay.xul");
  214.   overlay2.remove();
  215.  
  216.   var overlay3 = new overlayRemover(chromeUrl + 
  217.                  "overlayinfo/browser/content/overlays.rdf",
  218.                  "chrome://browser/content/browser.xul",
  219.                  "chrome://googlebar/content/googlebarOverlay.xul");
  220.   overlay3.remove();
  221.  
  222.   var overlay4 = new overlayRemover(chromeUrl + 
  223.                  "overlayinfo/browser/content/overlays.rdf",
  224.                  "chrome://browser/content/pref/preftree.xul",
  225.                  "chrome://googlebar/content/googlebarPrefOverlay.xul");
  226.   overlay4.remove();
  227.  
  228.  
  229.  
  230.   // delete googlebar directory
  231.   var ios = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces['nsIIOService']);
  232.   var fileUri = ios.newURI(chromeUrl + "googlebar", null, null);
  233.   fileUri = fileUri.QueryInterface(Components.interfaces.nsIFileURL);
  234.   dump("\tDeleting googlebar directory: \n\t" + fileUri.spec + "\n");
  235.   try {
  236.     fileUri.file.remove(true);
  237.     dump("\tgooglebar directory deleted OK\n");
  238.   }
  239.   catch (err) {
  240.     dump("\tgooglebar directory NOT deleted. ERROR:\n" + err + "\n");
  241.     alert(dirNotDeleted + chromeDir.path);
  242.   }
  243.   semaphore--; // asyncronous operation finished
  244. }
  245.  
  246. // removeFromChrome is listener that removes all googlebar references from 
  247. // chrome/chrome.rdf when this DataSource is loaded
  248. var removeFromChrome = {
  249.   onDataSourceReady: function(aDS) 
  250.   {
  251.     dump("Googlebar: removeFromChrome\n");
  252.  
  253.     // get the sequence that holds all the packages
  254.     var rootSeq = RDFU.findSeq(aDS, "urn:mozilla:package:root");
  255.  
  256.     // get resource and node for your package
  257.     var myResource = gRDF.GetResource("urn:mozilla:package:googlebar");
  258.     var myNode = myResource.QueryInterface(Components.interfaces.nsIRDFNode);
  259.  
  260.     // and snip out your arc
  261.     rootSeq.RemoveElement(myNode, true);
  262.  
  263.     // now remove everything else we know about your package
  264.     var arcs = aDS.ArcLabelsOut(myResource);
  265.  
  266.     while(arcs.hasMoreElements()) {
  267.       var arc = arcs.getNext();
  268.     
  269.       // each arc is a property
  270.       var prop = arc.QueryInterface(Components.interfaces.nsIRDFResource);
  271.  
  272.       // For each property, get all targets, and unassert.  nested 
  273.       // enumeration is the best!
  274.       var targets = aDS.GetTargets(myResource, prop, true);
  275.  
  276.       while(targets.hasMoreElements()) {
  277.         var target = targets.getNext();
  278.  
  279.         var targetNode = target.QueryInterface(Components.interfaces.nsIRDFNode);
  280.         aDS.Unassert(myResource, prop, targetNode);
  281.       }
  282.     }
  283.  
  284.     // now flush the datasource back to disk
  285.     RDFU.saveDataSource(aDS);
  286.     dump("Googlebar: removeFromChrome OK\n");
  287.     semaphore--; // asyncronous operation finished
  288.   },
  289.  
  290.   onError: function(aStatus, aErrorMsg)
  291.   {
  292.     dump("Googlebar: removeFromChrome ERROR: status="+aStatus+",", aErrorMsg);
  293.   }
  294. };
  295.  
  296.  
  297. /*
  298.   function SelectLocaleForPackage(package_in) 
  299.   { 
  300.       try {
  301.       alert("Ready to Update Current user profile to en-US for Googlebar.");
  302.  
  303.       var chromeRegistry = Components.classes["@mozilla.org/chrome/chrome-registry;1"]
  304.         .getService(Components.interfaces.nsIXULChromeRegistry);
  305.  
  306.       if (chromeRegistry) {
  307.                 alert("Got chrome, this is looking good so far.");
  308.                 chromeRegistry.selectLocaleForPackage( "en-US", package_in, true);
  309.                 chromeRegistry.reloadChrome();
  310.                 chromeRegistry.checkForNewChrome();
  311.                 alert("You should see a Googlebar.");
  312.                 
  313.               }// I only missed here once, and restarting moz worked.
  314.                 else alert("Missed the chrome. Try shuting down Mozilla and trying this again.");
  315.  
  316.  
  317.         }// try failed 
  318.         catch(e) 
  319.         {         alert("Bigger Miss here:\n"+e); 
  320.                 alert("This happens if you invoke this fix without using the\n" +
  321.         "chrome:// prefix, eg.chrome://googlebar/content/install_fix.xul"); }
  322.  
  323.   }
  324.  
  325. */
  326.  
  327.  
  328.  
  329.